<?php


namespace frontend\controllers;


use common\models\elasticsearch\entity\Article;
use cza\base\models\statics\EntityModelStatus;
use Yii;
use yii\elasticsearch\Exception;
use yii\web\Controller;
use yii\web\Response;

class EsController extends Controller
{

    /**
     * @return bool
     */
    public function actionPut()
    {
        $model = new Article();
        $model->primaryKey = 3; // in this case equivalent to $customer->id = 1;
        $model->title = 'im title';
        $model->description = 'im description';
        $model->content = 'im content';
        $model->status = EntityModelStatus::STATUS_ACTIVE;
        $model->created_by = 1;
        $model->updated_by = 1;
        return $model->save();
    }

    public function actionDelete()
    {
        Article::deleteIndex();
    }

    public function actionGet($title = '')
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $result = Article::find()
            ->query(["match" => ["title" => $title]])
            ->all(); // articles whose title contains "yii"
        return $result;
    }

    public function actionHealth()
    {
        return Yii::$app->elasticsearch->get('/_cat/health?v');
    }
}